@extends('admin.layout')
@section('title', '后台管理员')
@section('content')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">登录名</label>
                        <div class="layui-input-block">
                            <input type="text" name="loginname" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">手机</label>
                        <div class="layui-input-block">
                            <input type="text" name="telphone" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-block">
                            <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <select name="role">
                                <option value="0">管理员</option>
                                <option value="1">超级管理员</option>
                                <option value="2">纠错员</option>
                                <option value="3">采购员</option>
                                <option value="4">推销员</option>
                                <option value="5">运营人员</option>
                                <option value="6">编辑</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    @can('user.admin.destroy')
                        <button class="layui-btn layui-btn-danger layuiadmin-btn-admin" data-type="batchdel">删除</button>
                    @endcan
                    @can('user.admin.create')
                        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
                    @endcan
                </div>

                <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
                <script type="text/html" id="table-useradmin-admin">
                    <div class="layui-btn-group">
                        @can('user.admin.create')
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        @endcan
                        @can('user.admin.role')
                            <a class="layui-btn layui-btn-xs" lay-event="role">角色</a>
                        @endcan
                        @can('user.admin.destroy')
                            @{{#  if(d.id == 1){ }}
                            <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
                            @{{#  } else { }}
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                            @{{#  } }}
                        @endcan
                    </div>
                </script>
            </div>
        </div>
    </div>
@endsection
@section('js')
    <script>
        layui.config({
            base: '/static/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'useradmin', 'table'], function(){
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table;

            //监听搜索
            form.on('submit(LAY-user-back-search)', function(data){
                var field = data.field;

                //执行重载
                table.reload('LAY-user-back-manage', {
                    where: field
                });
            });

            //事件
            var active = {
                batchdel: function(){
                    var checkStatus = table.checkStatus('dataTable')
                        ,checkData = checkStatus.data; //得到选中的数据

                    if(checkData.length === 0){
                        return layer.msg('请选择数据');
                    }

                    layer.prompt({
                        formType: 1
                        ,title: '敏感操作，请验证口令'
                    }, function(value, index){
                        layer.close(index);
                        layer.confirm('确定删除吗？', function(index) {
                            $.post("{{ route('admin.user.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code===0){
                                    table.reload('dataTable');
                                    //location.reload(); //删除成功后再刷新
                                }
                                layer.close(index);
                                layer.msg(result.msg,{icon:6})
                            });

                            //执行 Ajax 后重载
                            /*
                            admin.req({
                              url: 'xxx'
                              //,……
                            });
                            */
                        });
                    });
                }
                ,add: function(){
                    layer.open({
                        type: 2
                        ,title: '添加管理员'
                        ,content: "{{ route('admin.admin.create') }}"
                        ,area: ['420px', '420px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url:"{{route('admin.admin.store')}}",
                                    data:field,
                                    type:"post",
                                    dataType:"json",
                                    success:function(data){
                                        if(data.code===0){
                                            layer.msg(data.msg, {icon: 6});
                                            table.reload('dataTable'); //数据刷新
                                            layer.close(index); //关闭弹层
                                        }else{
                                            layer.msg(data.msg, {icon: 5});
                                        }
                                    },
                                    error:function(data){
                                        layer.msg(data.msg);
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                }
            }
            $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
@endsection
